package com.example.inet;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author readpage
 * @create 2022-10-19 20:46
 * 实现TCP的网络编程
 * 例题: 从客户端发送文件给服务端，服务的保存到本地。并返回"发送成功"给客户端。
 * 并关闭相应的连接
 */
public class TCPTest3 {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1. 创建Socket
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
            //2. 获取输出流
            os = socket.getOutputStream();
            //3. 获取输入流
            fis = new FileInputStream("avatar.webp");

            //读写过程
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fis.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            //关闭数据的输出
            socket.shutdownOutput();


            //5.接收到来自服务端的数据，并显示到控制台上
            is = socket.getInputStream();

            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[20];
            int len2;
            while ((len2 = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len2);
            }
            System.out.println(baos.toString());


        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭流
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void server() {
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            ServerSocket ss = new ServerSocket(9000);

            socket = ss.accept();

            is = socket.getInputStream();

            fos = new FileOutputStream("avatar7.webp");

            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }

            //服务器端给予客户端反馈
            os = socket.getOutputStream();
            os.write("你好呀!".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
